package com.yiren.dbaa.module.help_center.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.constant.StringConstant;
import com.yiren.dbaa.net.oss.IOssFileOperationApi;
import com.yiren.dbaa.net.interf.OssFileCallback;
import com.yiren.dbaa.net.oss.impl.OssFileOperationApiImpl;
import com.yiren.dbaa.module.help_center.api.IHardwareUpdate;
import com.yiren.dbaa.module.help_center.api.impl.HardwareUpdateImpl;
import com.yiren.dbaa.module.help_center.bean.DeviceUpdateBean;
import com.yiren.dbaa.module.help_center.contract.UpdateContract;
import com.yiren.dbaa.util.DeviceUtil;

/**
 * @author lwb
 * @date :2022/7/27 15:25
 * @description: 更新P
 */
public class UpdatePresenter implements UpdateContract.Presenter {

    private Activity activity;
    private IOssFileOperationApi iOssFileOperationApi;
    private IHardwareUpdate iHardwareUpdate;
    private boolean isShowSoftBt = false;
    private String updateSoftContext = "";
    private String hardWareVersion;
    private boolean isShowHardwaretBt = false;
    private String updateHardwareContext = "";

    public UpdatePresenter(Activity activity) {
        this.activity = activity;
        iOssFileOperationApi = new OssFileOperationApiImpl(activity);
        iHardwareUpdate = new HardwareUpdateImpl(activity);
    }

    @Override
    public void getSoftUpdateInfo(UpdateContract.View view) {
        String appVersion = DeviceUtil.getAppVersion(activity);
        int appVersionCode = DeviceUtil.getAppCode(activity);

        iOssFileOperationApi.downLoadTxtFile(StringConstant.DJ_APK, AddressConstant.APK_CONFIG_URL, new OssFileCallback() {
            @Override
            public void success(String result) {
                DeviceUpdateBean apkUpdateBean = new Gson().fromJson(result, DeviceUpdateBean.class);
                if (appVersionCode < Integer.parseInt(apkUpdateBean.getVersioncode())) {
                    isShowSoftBt = true;
                    updateSoftContext = apkUpdateBean.getVersionname() + "更新说明：\n";
                    for (int i = 0; i < apkUpdateBean.getContexts().size(); i++) {
                        updateSoftContext += (i + 1) + "、" + apkUpdateBean.getContexts().get(i) + "\n";
                    }
                }

                activity.runOnUiThread(() -> view.showSoftUpdateInfo(appVersion, updateSoftContext, isShowSoftBt));
            }
        });
        view.showSoftUpdateInfo(appVersion, updateSoftContext, isShowSoftBt);
    }

    @Override
    public void getHardwareUpdateInfo(UpdateContract.View view) {
        if (null != G.connectDeviceMessage) {
            hardWareVersion = G.connectDeviceMessage.getVersionDescribe();
            int versionCodeByHost = Integer.parseInt(G.connectDeviceMessage.getVersionCodeByHost());
            iOssFileOperationApi.downLoadTxtFile(StringConstant.DJ_APK, iHardwareUpdate.getUpgradesInstructions(), new OssFileCallback() {
                @Override
                public void success(String result) {
                    DeviceUpdateBean deviceUpdateBean = new Gson().fromJson(result, DeviceUpdateBean.class);
                    if (versionCodeByHost < Integer.parseInt(deviceUpdateBean.getVersioncode())) {
                        isShowHardwaretBt = true;
                        updateHardwareContext = deviceUpdateBean.getVersionname() + "更新说明：\n";
                        for (int i = 0; i < deviceUpdateBean.getContexts().size(); i++) {
                            updateHardwareContext += (i + 1) + "、" + deviceUpdateBean.getContexts().get(i) + "\n";
                        }
                    }
                    activity.runOnUiThread(() -> view.showHardwareUpdateInfo(hardWareVersion, updateHardwareContext, isShowHardwaretBt));
                }
            });
        }
        view.showHardwareUpdateInfo(hardWareVersion, updateHardwareContext, isShowHardwaretBt);
    }
}
